package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketSearchHistory;
import com.cskaoyan.market.service.AdminSearchHistoryService;
import com.cskaoyan.market.service.impl.AdminSearchHistoryServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/history/*")
public class AdminSearchHistoryController extends HttpServlet {

    AdminSearchHistoryService adminSearchHistoryService=new AdminSearchHistoryServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/history/", "");

        if ("list".equals(op)){
            list(req, resp);
        }else {
           resp.sendError(404);
           // resp.setStatus(404);
           // resp.sendRedirect(req.getContextPath() + "/admin/404.html");
           // resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
        }

    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String keyword = req.getParameter("keyword");
        String from = req.getParameter("from");
        String addTime = req.getParameter("add time");
        String updateTime=req.getParameter("update time");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");

        List<MarketSearchHistory> list = adminSearchHistoryService.list(id, username, keyword, from, addTime, updateTime,page,limit);

        Object o = ResponseUtil.okList(list);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
        resp.getWriter().println(s);


    }
}
